package org.zn.plus.mybatis.sql.builder.data.adapter;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.zn.commons.reflection.ObjectInvoker;
import org.zn.commons.tree.TreeNode;
import org.zn.plus.mybatis.sql.builder.model.Fragment;
import org.zn.plus.mybatis.sql.builder.model.Paramterable;
import org.zn.plus.mybatis.sql.builder.model.QueryTable;

public class MapSqlFragmentAdapter extends AbsrtactMappingFragmentAdapter<Map<String, Object>> {
	
	@Override
	public Fragment handleData(Map<String, Object> data) throws Exception {
		
		AdapterMappingNode root=adapterMappingTree.getRoot();
		
		Map<String, Object> queryData=	(Map<String, Object>) data.get(root.getNodeName());
		
		QueryTable mtable=new QueryTable();
				
		for (TreeNode<String> node : root.getChildrenNodes()) {
			
			doNode((AdapterMappingNode)node, queryData,mtable);

		}
		
		return mtable;
	}
	
	
	@SuppressWarnings("unchecked")
	private void doNode(AdapterMappingNode nodeMapping,Map<String, Object> nodeData,Fragment pmodel) throws Exception {
		
		if(nodeData==null) return;
		
		String nodeName =nodeMapping.getNodeName();
		
	//	System.out.println(nodeName);
		
		String column= nodeMapping.getColumn()==null?nodeName:nodeMapping.getColumn();
		
		String model=nodeMapping.getModel();
		
		String dataType=nodeMapping.getType()==null?"string":nodeMapping.getType(); 
	
		Object ref=nodeMapping.getRef();
		
		Class<Fragment> modelClazz=null;
		
		List<TreeNode<String>> chs=nodeMapping.getChildrenNodes();
		if(chs==null) chs=Collections.EMPTY_LIST;
		
		ObjectInvoker wrapper=ObjectInvoker.wrapper(pmodel);

		
		
		if(ref!=null) {
			
		}
		
		if(model!=null) {
			
			if(model.split("\\.").length==1)  model=basepackage+"."+model;
			
			 modelClazz=(Class<Fragment>) Class.forName(model);
			
		}
		
		// nodeData instanceof List  处理 数组数据的   模型和 值
		if("list".equals(dataType.toLowerCase())) {

			List<?> listNode= (List<?>) nodeData.get(nodeName);
			
			if(listNode==null)return;
			
			for (Object d : listNode) {
				
			if(modelClazz!=null) {
			
				Fragment ins= modelClazz.newInstance();

				for (TreeNode<String> treeNode : chs) {
					
					doNode((AdapterMappingNode)treeNode, (Map<String, Object>)d, ins);
				
				}
				
				wrapper.invokeMethod("add"+column.substring(0, 1).toUpperCase()+column.substring(1), ins);	
				
			}
			else {
			
				wrapper.invokeMethod("add"+column.substring(0, 1).toUpperCase()+column.substring(1), d);	
				
				}
				
			}
			

		}
		//单模型处理
		else if(modelClazz!=null) {
	
				Map<String, Object> nextData=(Map<String, Object>) nodeData.get(nodeName);
				
				Fragment ins= modelClazz.newInstance();
		
				for (TreeNode<String> treeNode : chs) {
			
					doNode((AdapterMappingNode)treeNode, nextData, ins);
			
				}
				
				wrapper.invokeMethod(ObjectInvoker.settterName(column), ins);

		}
		// 查询参数处理
		else if("parameter".equals(dataType.toLowerCase())){
			
		Object  parameter=nodeData.get(nodeName);
		
		if(pmodel instanceof Paramterable) {
			
			Paramterable  pable =(Paramterable) pmodel;
		
			pable.setParamter(parameter);
			
		}
			
		}
		//单数值
		else {
			
			
			Object value=nodeData.get(nodeName);
			
			//System.out.println(column +" " +  nodeName +" v: "+value);
			
			if(value!=null)
			wrapper.invokeMethod(ObjectInvoker.settterName(column),value);
			
		}
		
	}
	
	
	
}
